package br.com.ravi.deposito.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.ravi.deposito.dao.GenericDAO;
import br.com.ravi.deposito.exceptions.DAOException;
import br.com.ravi.deposito.vo.UserVO;

import com.google.gson.Gson;

@WebServlet(name="userServlet", urlPatterns="/UserAction/*")
public class UserAction extends AbstractAction {
	private static final long serialVersionUID = -3909361581697978310L;
	
	public void listUsers(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		req.getRequestDispatcher("/pages/users/listUsers.jsp").forward(req, resp);
	}
	
	public void getUsers(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, DAOException {
		Gson gson = new Gson();
		
		GenericDAO<UserVO> dao = new GenericDAO<UserVO>(UserVO.class);
		List<UserVO> userList = dao.list();
		resp.setContentType("application/json");
		PrintWriter writer = resp.getWriter();
		writer.println(gson.toJson(userList.toArray()));
	}
}